﻿package Modules.CommunicationsModule
{
	import Modules.CommunicationsModule.ClientServerCommunications;
	import Modules.MainTimeLine;
	import Modules.PlayerModule.Friend;
	import Modules.PlayerModule.Player;
	
	import com.adobe.serialization.json.*;

	public class Session
	{
		//Reference variable for the game's player
		public var player:Player;
		public var newPlayer:Boolean;
		
		//Reference variable for the friend whom the player is visiting
		public var friendPlayer:Player;
		
		//Log in time of this player
		public var loginTime:Player;
		
		//Session identification
		//Not much use now, but will be useful to have in the future
		//We can track all transactions using session id.
		public var sessionid:int;
		
		//constructor
		public function Session()
		{
			newPlayer = true;
			MainTimeLine.friends = [];
		}
		
		//Start up function
		//Call this function to start the entire game
		public function initSession():void{
		
			var paramsObj:Object = MainTimeLine.loaderInfo.parameters;
			
			//create a new player object using the player's facebook ID
			player =  new Player("641954941"); //new Player(paramsObj["uid0"]); // new Player("688267373");//
			sessionid = paramsObj["sessionid"];
			
			var i:int;
			
			for(i=1; i<paramsObj["length"]; i++){
				
				var friend:Friend = new Friend();
				friend.image = paramsObj["image"+i];
				friend.uid = paramsObj["uid"+i];
				friend.friendName = paramsObj["name"+i];
				MainTimeLine.friends.push(friend);
			}
			
			player.initTownHall();
			MainTimeLine.friendPanel.initFriendPanel();
			ClientServerCommunications.initPlayerSetUp();
		}
		
		public function save():void{
			
			ClientServerCommunications.savePlayerValues(player);
			ClientServerCommunications.savePlayerInventory(player);
			ClientServerCommunications.saveFarmRequest(player);
		}
	}
}